package cn.itcast.sys.controller.admin;

import cn.itcast.constant.ShopConstant;
import cn.itcast.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/admin/shop")
@Api(tags = "店铺相关接口")
@Slf4j
public class ShopController {
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取营业状态
     * @return
     */
    @GetMapping("/status")
    @ApiOperation("获取营业状态")
    public Result<Integer> getStatus() {
        log.info("获取营业状态");
        Integer status = (Integer) redisTemplate.opsForValue().get(ShopConstant.SHOP_STATUS_REDIS_KEY);
        if (status == null) {
            redisTemplate.opsForValue().set(ShopConstant.SHOP_STATUS_REDIS_KEY, 0);
            redisTemplate.expire(ShopConstant.SHOP_STATUS_REDIS_KEY, 30, TimeUnit.DAYS);
            return Result.success(0);
        }
        return Result.success(status);
    }

    /**
     * 设置营业状态
     * @param status
     * @return
     */
    @PutMapping("/{status}")
    @ApiOperation("设置营业状态")
    public Result<String> setStatus(@PathVariable Integer status) {
        log.info("设置营业状态：{}", status);
        redisTemplate.opsForValue().set(ShopConstant.SHOP_STATUS_REDIS_KEY, status);
        redisTemplate.expire(ShopConstant.SHOP_STATUS_REDIS_KEY, 30, TimeUnit.DAYS);
        return Result.success();
    }
}

